package com.mlethe.library.fingerprint;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorRes;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;

import com.mlethe.library.fingerprint.entity.VerificationDialogStyle;

/**
 * @author Mlethe
 */
class FingerprintDialog {

    private Context mContext;

    private OnDialogActionListener actionListener;

    private AlertDialog alertDialog;

    private TextView tvTip;

    public FingerprintDialog(Context context, VerificationDialogStyle verificationDialogStyle) {
        mContext = context;
        View view = LayoutInflater.from(context).inflate(R.layout.biometricprompt_layout_fingerprint_dialog, null, false);
        alertDialog = new AlertDialog.Builder(context)
                .setView(view)
                .setCancelable(false)
                .setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        if (actionListener != null) {
                            actionListener.onCancel();
                        }
                        tvTip = null;
                        alertDialog = null;
                        actionListener = null;
                        mContext = null;
                    }
                })
                .create();
        tvTip = view.findViewById(R.id.tvTip);
        ImageView ivFingerprint = view.findViewById(R.id.ivFingerprint);
        TextView tvCancel = view.findViewById(R.id.tvCancel);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (actionListener != null) {
                    actionListener.onCancel();
                }
                if (alertDialog != null) {
                    alertDialog.cancel();
                }
            }
        });

        // 调用者定义验证框样式
        if (verificationDialogStyle == null) {
            return;
        }
        if (verificationDialogStyle.getCancelTextColor() != 0) {
            tvCancel.setTextColor(verificationDialogStyle.getCancelTextColor());
        }
        if (verificationDialogStyle.getFingerprintColor() != 0) {
            Drawable wrappedDrawable = DrawableCompat.wrap(ivFingerprint.getDrawable());
            DrawableCompat.setTintList(wrappedDrawable, ColorStateList.valueOf(verificationDialogStyle.getFingerprintColor()));
            ivFingerprint.setImageDrawable(wrappedDrawable);
        }
        if (!TextUtils.isEmpty(verificationDialogStyle.getCancelBtnText())) {
            tvCancel.setText(verificationDialogStyle.getCancelBtnText());
        }
    }

    /**
     * 显示
     */
    public void show() {
        if (alertDialog != null) {
            alertDialog.show();
        }
    }

    /**
     * 关闭
     */
    public void cancel() {
        if (alertDialog != null) {
            alertDialog.cancel();
        }
    }

    public FingerprintDialog setActionListener(OnDialogActionListener actionListener) {
        this.actionListener = actionListener;
        return this;
    }

    /**
     * 根据指纹验证的结果更新tip的文字内容和文字颜色
     *
     * @param strId
     * @param colorId
     */
    public void setTip(@StringRes int strId, @ColorRes int colorId) {
        if (tvTip != null) {
            tvTip.setText(strId);
            if (mContext != null) {
                tvTip.setTextColor(ContextCompat.getColor(mContext, colorId));
            }
        }
    }

    /**
     * 根据指纹验证的结果更新tip的文字内容和文字颜色
     *
     * @param str
     * @param colorId
     */
    public void setTip(String str, @ColorRes int colorId) {
        if (tvTip != null) {
            tvTip.setText(str);
            if (mContext != null) {
                tvTip.setTextColor(ContextCompat.getColor(mContext, colorId));
            }
        }
    }

    public interface OnDialogActionListener {
        void onCancel();
    }

}
